/**
	Shiokaze core libraries
	Copyright (c) 2011 Shiokaze development team

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <Desktop.hh>

#include <unistd.h>
#include <stdlib.h>

#include <QtCore/QSettings>

static void _fehSetWallpaper(const QString& path) {
	pid_t pid = vfork();
	if (pid == 0) {
		// Use feh to set background image
		execl("/usr/bin/feh", "/usr/bin/feh", "--bg-scale",
			  path.toStdString().c_str(), (char*)0);
		_exit(0);
	}
}


void Desktop::loadInitialConfig() {
	// Load settings
	QString path = getenv("HOME");
	path += "/.shiokaze/desktop.conf";
	QSettings settings(path, QSettings::NativeFormat);

	// Default wallpaper
	QString wallpaper = settings.value("LookAndFeel/Wallpaper",
									   "/usr/share/shiokaze/data/default.jpg").toString();
	_fehSetWallpaper(wallpaper);
}

void Desktop::setWallpaper(const QString &path) {
	// Set actual wallpaper
	_fehSetWallpaper(path);

	// Load settings
	QString cfg = getenv("HOME");
	cfg += "/.shiokaze/desktop.conf";
	QSettings settings(cfg, QSettings::NativeFormat);
	settings.setValue("LookAndFeel/Wallpaper", path);
	settings.sync();
}
